package org.erepublik.company;

import java.util.ArrayList;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

public class Hospital implements JSONAble, Comparable<Hospital> {


    private long usageTime;
    private int region;
    private int wellness;
    private int wellnessPerPlayer;
    private Battle battle;
    private String regionName;
    
    public Battle getBattle() {
        return battle;
    }
    public void setBattle(Battle battle) {
        this.battle = battle;
    }
    public int getRegion() {
        return region;
    }
    public void setRegion(int region) {
        this.region = region;
    }
    public int getWellness() {
        return wellness;
    }
    public void setWellness(int wellness) {
        this.wellness = wellness;
    }
    public int getWellnessPerPlayer() {
        return wellnessPerPlayer;
    }
    public void setWellnessPerPlayer(int wellnessPerPlayer) {
        this.wellnessPerPlayer = wellnessPerPlayer;
    }
    public long getUsageTime() {
        return usageTime;
    }
    public void setUsageTime(long usageTime) {
        this.usageTime = usageTime;
    }
    
    
    public String getRegionNameWithDash() {
        return getRegionName().replace(" ", "-");
    }
    
    
    public JSONObject toJSONObject() throws JSONException {
        JSONObject object = new JSONObject();
        object.put("region", region);
        object.put("wellness", wellness);
        object.put("wellnessPerPlayer", wellnessPerPlayer);
        object.put("regionName", regionName);       
        return object;
    }
    

    public static Hospital fromJSONObject(JSONObject object) throws JSONException {
        Hospital region = new Hospital();
        region.setRegionName( object.getString("regionName") );
        region.setWellness( object.getInt("wellness") );
        region.setWellnessPerPlayer( object.getInt("wellnessPerPlayer") );
        region.setRegion( object.getInt("region") );
        
        return region;
    }
    
    
    
    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }
    public String getRegionName() {
        return regionName;
    }
    
    
    public ArrayList<ECountry> getAvailableEdenCountries() {
        ArrayList<ECountry> list = new ArrayList<ECountry>();
        if ( battle.getDefender().getAlliance() == EAlliance.EDEN )
            list.add( battle.getDefender() );
        for (ECountry eCountry : battle.getDefendersAllies()) {
            if ( eCountry.getAlliance() == EAlliance.EDEN )
                list.add( eCountry );
        }
        
        return list;
    }
    
    
    public ArrayList<ECountry> getAvailablePhoenixCountries() {
        ArrayList<ECountry> list = new ArrayList<ECountry>();
        if ( battle.getDefender().getAlliance() == EAlliance.PHOENIX )
            list.add( battle.getDefender() );
        for (ECountry eCountry : battle.getDefendersAllies()) {
            if ( eCountry.getAlliance() == EAlliance.PHOENIX )
                list.add( eCountry );
        }
        return list;
    }
    
    
    public ArrayList<ECountry> getAvailableNeutralCountries() {
        ArrayList<ECountry> list = new ArrayList<ECountry>();
        if ( battle.getDefender().getAlliance() == EAlliance.NEUTRAL ||
                battle.getDefender().getAlliance() == EAlliance.NEW_COUNTRIES)
            list.add( battle.getDefender() );
        for (ECountry eCountry : battle.getDefendersAllies()) {
            if ( eCountry.getAlliance() == EAlliance.NEUTRAL ||
                    eCountry.getAlliance() == EAlliance.NEW_COUNTRIES)
                list.add( eCountry );
        }
        return list;
    }
    
    
    public String getRemainingTimeAsString() {
        long now = new Date().getTime()-1000*60*60*Configuration.serverTimeDifference;
        long timeRemaining = getUsageTime() - now;
        
        if (timeRemaining < 0 && timeRemaining > -1000*60*60*2 )
            return "Now!";
        if (timeRemaining < -1000*60*60*2)
            return "cos sie spierdolilo";
        
        int godzin = (int) (timeRemaining/(1000*60*60));
        int minutes = (int) ((timeRemaining%(1000*60*60)) / (1000*60));
        return godzin+" h "+minutes+" min ";
    }
    
    
    @Override
    public ECountry getCountry() {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public int compareTo(Hospital o) {

        return (int) (getUsageTime() - o.getUsageTime());
    }
    
    
    
}
